package com.zhilei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhilei.base.BaseInfoProperties;
import com.zhilei.exceptions.GraceException;
import com.zhilei.grace.result.ResponseStatusEnum;
import com.zhilei.mapper.IndustryMapper;
import com.zhilei.mapper.IndustryVOMapperCustom;
import com.zhilei.pojo.Industry;
import com.zhilei.pojo.bo.IndustryBO;
import com.zhilei.pojo.vo.TopIndustryWithThirdListVO;
import com.zhilei.service.IndustryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 行业表 服务实现类
 * </p>
 *
 * @author 志磊
 * @since 2024-09-22
 */
@Service
public class IndustryServiceImpl extends BaseInfoProperties implements IndustryService {

    public static final Integer ZERO = 0;

    @Autowired
    private IndustryMapper industryMapper;

    @Autowired
    private IndustryVOMapperCustom industryVOMapperCustom;

    /**
     * 根据行业名称查询行业是否存在
     *
     * @param name
     * @return
     */
    @Override
    public boolean getIndustryIsExistByName(String name) {
        Industry industry = industryMapper.selectOne(new QueryWrapper<Industry>().eq("name", name));
        return industry != null;
    }

    /**
     * 创建检点名称
     *
     * @param industryBO
     */
    @Transactional
    @Override
    public void createNode(IndustryBO industryBO) {

        Industry industry = new Industry();
        BeanUtils.copyProperties(industryBO, industry);
        industryMapper.insert(industry);

    }

    /**
     * 查询所有一级分类的数据
     *
     * @param industryByFatherId
     * @return
     */
    @Override
    public List<Industry> getIndustryIsExistByFatherId(String industryByFatherId) {

        List<Industry> industryList = industryMapper.selectList(new QueryWrapper<Industry>()
                .eq("father_id", industryByFatherId));
        return industryList;
    }

    /**
     * 更新节点信息
     *
     * @param industryBO
     */
    @Transactional
    @Override
    public void updateIndustry(IndustryBO industryBO) {
        String industryBOId = industryBO.getId();
        if (StringUtils.isBlank(industryBOId)) {
            GraceException.disPlay(ResponseStatusEnum.USER_STATUS_ERROR);
        }

        Industry industry = industryMapper.selectById(industryBOId);
        BeanUtils.copyProperties(industryBO, industry);
        industryMapper.updateById(industry);
    }

    /**
     * 删除节点
     *
     * @param industryByFatherId
     */
    @Transactional
    @Override
    public void deleteIndustry(String industryByFatherId) {
        industryMapper.delete(new QueryWrapper<Industry>()
                .eq("id", industryByFatherId));
    }

    /**
     * 根据当前id查询节点
     *
     * @param industryByFatherId
     * @return
     */
    @Override
    public Industry getIndustryById(String industryByFatherId) {
        Industry industry = industryMapper.selectOne(new QueryWrapper<Industry>()
                .eq("id", industryByFatherId));
        return industry;
    }

    /**
     * 根据id查询当前的节点下是否还有其他的节点
     *
     * @param industryByFatherId
     * @return
     */
    @Transactional
    @Override
    public List<Industry> getIndustryBySecondId(String industryByFatherId) {
        List<Industry> industryBySecondIdList = industryMapper.getIndustryBySecondId(industryByFatherId);
        if (industryBySecondIdList == null || industryBySecondIdList.isEmpty()) {
            return Collections.emptyList();
        }

        for (Industry industry : industryBySecondIdList) {
            String fatherId = industry.getFatherId();
            if (StringUtils.isBlank(fatherId)) {
                // TODO 返回哟个空列表
                return Collections.emptyList();
            }
        }
        return industryBySecondIdList;
    }

    /**
     * 查询顶级节点列表
     *
     * @return
     */
    @Override
    public List<Industry> getTopIndustryList() {
        List<Industry> industryList = industryMapper.selectList(new QueryWrapper<Industry>()
                .eq("father_id", ZERO));
        return industryList;
    }

    /**
     * 根据顶级分类id查询三级分类id
     *
     * @param thirdListByTopId
     * @return
     */
    @Override
    public List<Industry> getThirdListByTopId(String thirdListByTopId) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("thirdListByTopId", thirdListByTopId);

        List<Industry> industryBySecondId = industryVOMapperCustom.getIndustryThirdListByTopId(map);

        return industryBySecondId;
    }

    /**
     * 根据三级节点的id查询一级节点的数据
     *
     * @param thirdListByTopId
     * @return
     */
    @Override
    public String getTopIndustryId(String thirdListByTopId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("thirdListByTopId", thirdListByTopId);
        String thirdListByTopIdStr = industryVOMapperCustom.getTopIndustryId(map);
        return thirdListByTopIdStr;
    }

    /**
     * 查询三级分类列表
     *
     * @return
     */
    @Override
    public List<TopIndustryWithThirdListVO> getAllThirdIndustry() {
        List<TopIndustryWithThirdListVO> allThirdIndustryList = industryVOMapperCustom.getAllThirdIndustryList();
        return allThirdIndustryList;
    }
}
